package com.lagou.controller;

import com.lagou.feign.GoodsFeign;
import com.lagou.pojo.Products;
import com.lagou.pojo.ProductsVo;
import com.lagou.pojo.ResultVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 5:45 下午 2021/9/15
 */
@RestController
@RequestMapping("/goods")
public class ProductsController {
    /*private String host;
    private int port;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/query/{id}")
    public ResultVo query(@PathVariable Integer id) {
        ResultVo resultVo = new ResultVo();
        getHostAndPort();
        String url = "http://" + host + ":" + port + "/product/query/" + id;
        //4.调用
        Products products = restTemplate.getForObject(url, Products.class);
        if (products != null) {
            resultVo.setStatus(true);
            resultVo.setMessage("查询成功");
            resultVo.setContent(products.toString());
        } else {
            resultVo.setStatus(false);
            resultVo.setMessage("查询失败");
            resultVo.setContent(null);
        }
        return resultVo;
    }

    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable Integer id) {
        ResultVo resultVo = new ResultVo();
        getHostAndPort();
        String url = "http://" + host + ":" + port + "/product/delete/" + id;
        //4.调用
        restTemplate.delete(url, Integer.class);
        resultVo.setStatus(true);
        resultVo.setMessage("删除成功");
        return resultVo;
    }

    @PostMapping("saveOrUpdate")
    public ResultVo saveOrUpdate(@RequestBody Products products) {
        ResultVo resultVo = new ResultVo();
        getHostAndPort();
        String url = "http://" + host + ":" + port + "/product/saveOrUpdate/";
        //4.调用
        int i = restTemplate.postForObject(url, products, Integer.class);
        //根据id判断新增或更新
        if (products.getId() == null) {

            if (i != 0) {
                resultVo.setStatus(true);
                resultVo.setMessage("新增成功");
            } else {
                resultVo.setStatus(false);
                resultVo.setMessage("新增失败");
            }
        } else {
            if (i != 0) {
                resultVo.setStatus(true);
                resultVo.setMessage("更新成功");
            } else {
                resultVo.setStatus(true);
                resultVo.setMessage("更新失败");
            }
        }
        return resultVo;
    }

    @PostMapping("queryByPage")
    public ResultVo queryByPage(@RequestBody ProductsVo productsVo) {
        ResultVo resultVo = new ResultVo();
        getHostAndPort();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<ProductsVo> requestEntity = new HttpEntity<>(productsVo, httpHeaders);
        String url = "http://" + host + ":" + port + "/product/queryByPage/";
        ResponseEntity<Map> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Map.class);
        Map map=exchange.getBody();
        if (map != null) {
            resultVo.setStatus(true);
            resultVo.setMessage("查询成功");
            resultVo.setContent(map.toString());
        } else {
            resultVo.setStatus(false);
            resultVo.setMessage("查询失败");
            resultVo.setContent(null);
        }
        return resultVo;
    }

    public void getHostAndPort() {
        List<ServiceInstance> instances = discoveryClient.getInstances("lagou-service-goods");
        ServiceInstance serviceInstance = instances.get(0);
        host = serviceInstance.getHost();
        port = serviceInstance.getPort();
    }*/

    @Autowired
    private GoodsFeign goodsFeign;

    @GetMapping("/query/{id}")
    public ResultVo findDataById(@PathVariable Integer id) {
        ResultVo resultVo = new ResultVo();
        Products products = goodsFeign.query(id);
        if (products != null) {
            resultVo.setStatus(true);
            resultVo.setMessage("查询成功");
            resultVo.setContent(products.toString());
        } else {
            resultVo.setStatus(false);
            resultVo.setMessage("查询失败");
            resultVo.setContent(null);
        }
        return resultVo;
    }

    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable Integer id) {
        ResultVo resultVo = new ResultVo();
        int delete = goodsFeign.delete(id);
        if (delete != 0) {
            resultVo.setStatus(true);
            resultVo.setMessage("删除成功");
        } else {
            resultVo.setStatus(false);
            resultVo.setMessage("删除失败");
        }

        return resultVo;
    }


    @PostMapping("saveOrUpdate")
    public ResultVo saveOrUpdate(@RequestBody Products products) {
        ResultVo resultVo = new ResultVo();
        int i = goodsFeign.saveOrUpdate(products);
        //根据id判断新增或更新
        if (products.getId() == null) {
            if (i != 0) {
                resultVo.setStatus(true);
                resultVo.setMessage("新增成功");
            } else {
                resultVo.setStatus(false);
                resultVo.setMessage("新增失败");
            }
        } else {
            if (i != 0) {
                resultVo.setStatus(true);
                resultVo.setMessage("更新成功");
            } else {
                resultVo.setStatus(true);
                resultVo.setMessage("更新失败");
            }
        }
        return resultVo;
    }


    @PostMapping("queryByPage")
    public ResultVo queryByPage(@RequestBody ProductsVo productsVo) {
        ResultVo resultVo = new ResultVo();
        Map<String, Object> map = goodsFeign.queryByPage(productsVo);
        if (map != null) {
            resultVo.setStatus(true);
            resultVo.setMessage("查询成功");
            resultVo.setContent(map.toString());
        } else {
            resultVo.setStatus(false);
            resultVo.setMessage("查询失败");
            resultVo.setContent(null);
        }
        return resultVo;
    }
}
